package com.ztgf.user.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.Md5Utils;
import com.ztgf.common.utils.RandomValidateCodeUtil;
import com.ztgf.shopping.models.dto.shop.ShopBlacklistDTO;
import com.ztgf.shopping.models.dto.shop.ShopInfoDTO;
import com.ztgf.shopping.models.dto.shop.ShopReceiveCardDTO;
import com.ztgf.shopping.models.dto.shop.ShopUserRelationDTO;
import com.ztgf.shopping.models.service.shop.IShopInfoService;
import com.ztgf.shopping.models.service.shop.IShopReceiveCardService;
import com.ztgf.shopping.models.service.shop.IShopUserRelationService;
import com.ztgf.sys.models.service.ISysDictService;
import com.ztgf.user.models.bo.UserLoginBO;
import com.ztgf.user.models.bo.UserLoginSaveBO;
import com.ztgf.user.models.bo.UserLoginSubSaveBO;
import com.ztgf.user.models.bo.UserLoginUpdateBO;
import com.ztgf.user.models.constant.UserConstant;
import com.ztgf.user.models.dto.UserImDTO;
import com.ztgf.user.models.dto.UserInfoDTO;
import com.ztgf.user.models.dto.UserLoginDTO;
import com.ztgf.user.models.service.IUserInfoService;
import com.ztgf.user.models.service.IUserLoginService;
import com.ztgf.user.models.vo.UserInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 用户登录表控制器
 *
 * @author zhanghualang
 * @Date 2020-05-28 17:42:53
 */

@RestController
@RequestMapping("/api/userLogin")
@Slf4j
@Api(value = "UserLoginApiController", tags = "用户登录表Api接口")
public class UserLoginApiController {

    @DubboReference
    private IUserLoginService userLoginService;
    @DubboReference
    private ISysDictService iSysDictService;

    @DubboReference
    private IUserInfoService userInfoService;

    @DubboReference
    private IShopInfoService shopInfoService;
    @DubboReference
    private IShopReceiveCardService shopReceiveCardService;

    @DubboReference
    private IShopUserRelationService shopUserRelationService;

    @ApiOperation(value = "添加用户登录表", notes = "添加用户登录表")
    @PostMapping(value = "/insert")
    public Result insert(UserLoginSaveBO saveBO) {
        if (StringUtils.isEmpty(saveBO.getPassword()) || saveBO.getPassword().length() < 32) {
            return new Result<>(CodeEnum.FAIL_100002);
        }

        if (StringUtils.isEmpty(saveBO.getName())) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        saveBO.setName(saveBO.getName().trim());

        //验证码0注册验证码，1登录验证码，2修改密码验证码，3更改绑定手机验证码，4支付验证码，5保证金修改
        if (!userLoginService.getSmsCaptcha(saveBO.getMobile(), saveBO.getCaptcha(), 0)) {
            return new Result<>(CodeEnum.FAIL_Login_1004);
        }

        //验证码0注册验证码，1登录验证码，2修改密码验证码，3更改绑定手机验证码，4支付验证码，5保证金修改
        if (!userLoginService.getSmsCaptcha(saveBO.getPayMobile(), saveBO.getPayCaptcha(), 11)) {
            return new Result<>(CodeEnum.FAIL_Login_1009);
        }

        //验证是否已经被加入黑名单
        //黑名单类型 0用户黑名单，1店铺黑名单
        ShopBlacklistDTO shopBlacklistDTO = userLoginService.getByCompanyName(saveBO.getName(), 0);
        if (shopBlacklistDTO != null) {
            return new Result<>(CodeEnum.FAIL_AddUser_3006);
        }

        //验证公司名是否重复
        if (userLoginService.getLoginByName(saveBO.getName())) {
            return new Result<>(CodeEnum.FAIL_AddUser_3007);
        }

        //验证手机号是否重复
        if (userLoginService.updateByMobile(saveBO.getMobile())) {
            return new Result<>(CodeEnum.FAIL_AddUser_3004);
        }

        //验证邮箱是否重复
        if (userLoginService.updateByEmail(saveBO.getEmail())) {
            return new Result<>(CodeEnum.FAIL_AddUser_3005);
        }

        UserInfoDTO dto = userLoginService.add(saveBO);
        if (dto != null) {
            //发送邮件
            userLoginService.sendEmail(saveBO.getEmail(), dto.getId());
            return new Result<>(dto);
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @ApiOperation(value = "店铺添加子账户", notes = "店铺添加子账户")
    @PostMapping(value = "/insertSub")
    public Result insertSub(UserLoginSubSaveBO saveBO) {
        if (userLoginService.updateByMobile(saveBO.getMobile())) {
            return new Result<>(CodeEnum.FAIL_AddUser_3004);
        }

        if (!userLoginService.getSmsCaptcha(saveBO.getMainMobile(), saveBO.getCaptcha(), 0)) {
            return new Result<>(CodeEnum.FAIL_Login_1004);
        }

        boolean flag = userLoginService.addSub(saveBO);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @ApiOperation(value = "登录", notes = "登录")
    @PostMapping(value = "/login")
    public Result<HashMap<String, Object>> login(UserLoginBO userLoginBO) {
        if (StringUtils.isEmpty(userLoginBO.getAccount()) || StringUtils.isEmpty(userLoginBO.getPassword()) || StringUtils.isEmpty(userLoginBO.getCaptcha()) || StringUtils.isEmpty(userLoginBO.getRandomStr())) {
            return new Result<>(CodeEnum.FAIL_100002);
        }

        String saveCode = userLoginService.getVerifyWidth(RandomValidateCodeUtil.DEFAULT_CODE_KEY + userLoginBO.getRandomStr());

        if (StringUtils.isEmpty(saveCode)) {
            return new Result<>(CodeEnum.FAIL_Login_1008);
        }
        //误差在10px以内
        Integer saveCodeInt1 = Integer.parseInt(saveCode) - 10;
        Integer saveCodeInt2 = Integer.parseInt(saveCode) + 10;
        Integer codeInt = Integer.parseInt(userLoginBO.getCaptcha());

        if (saveCodeInt1 <= codeInt && saveCodeInt2 >= codeInt) {
            userLoginService.deleteVerifyWidth(RandomValidateCodeUtil.DEFAULT_CODE_KEY + userLoginBO.getRandomStr());
        } else {
            userLoginService.deleteVerifyWidth(RandomValidateCodeUtil.DEFAULT_CODE_KEY + userLoginBO.getRandomStr());
            return new Result<>(CodeEnum.FAIL_Login_1008);
        }

        HashMap<String, Object> map = new HashMap<String, Object>();
        UserLoginDTO userLoginDTO = userLoginService.getLogin(userLoginBO);
        if (userLoginDTO == null) {
            return new Result<>(CodeEnum.FAIL_USER_200003);
        }

        //验证是否被锁定
        if (userLoginDTO.getStatus().equals(1)) {
            Result result = new Result();
            result.setCode(1009);
            result.setMsg("您的账号已被锁定。请" + iSysDictService.getByName(UserConstant.UNLOCK_TIME).getValue() + "分钟后登录，或者联系管理员");
            return result;
        }
        userLoginDTO.setPassword(null);
        userLoginDTO.setSalt(null);
        map.put("userLogin", userLoginDTO);

        UserInfoDTO userInfoDTO = userLoginService.getUserInfo(userLoginBO);

        if (userInfoDTO == null) {
            //减少验证次数
            int verifyNum = userLoginService.lowerVerifyNum(userLoginDTO);
            Result result = new Result();
            if (verifyNum <= 0) {
                result.setCode(1009);
                result.setMsg("您的账号已被锁定。请" + iSysDictService.getByName(UserConstant.UNLOCK_TIME).getValue() + "分钟后登录，或者联系管理员");
                return result;
            } else {
                result.setCode(CodeEnum.FAIL_Login_1002.getCode());
                result.setMsg(CodeEnum.FAIL_Login_1002.getMsg() + "。剩余" + verifyNum + "次验证次数。次数使用完后将锁定您的账号");
            }
            return result;
        }

        if (userInfoDTO.getEmailActive() == 0) {
            HashMap<String, Object> umap = new HashMap<String, Object>();
            umap.put("userId", userInfoDTO.getId());
            umap.put("email", userLoginDTO.getEmail());
            return new Result<>(CodeEnum.FAIL_USER_200005.getCode(), CodeEnum.FAIL_USER_200005.getMsg(), umap);
        }

        if (userInfoDTO.getStatus() == 4 || userInfoDTO.getStatus() == 5) {
            //状态 0创建，1审核通过，2审核打回，3审核不通过,4冻结、5删除
            return new Result<>(CodeEnum.FAIL_USER_200004);
        }
        //判断是否绑卡
        if (userInfoDTO.getStatus() == 1) {
            //状态 0创建，1审核通过，2审核打回，3审核不通过,4冻结、5删除
            EntityWrapper<ShopInfoDTO> shopInfoDTOEntityWrapper = new EntityWrapper<>();
            shopInfoDTOEntityWrapper.eq("user_id",userInfoDTO.getId());
            ShopInfoDTO shopInfoDTO = shopInfoService.selectOne(shopInfoDTOEntityWrapper);
            if (shopInfoDTO==null){
                    EntityWrapper<ShopUserRelationDTO> shopUserRelationDTOEntityWrapper = new EntityWrapper<>();
                    shopUserRelationDTOEntityWrapper.eq("user_id",userInfoDTO.getId());
                    ShopUserRelationDTO shopUserRelationDTO = shopUserRelationService.selectOne(shopUserRelationDTOEntityWrapper);
                    if (shopUserRelationDTO!=null){
                        shopInfoDTO = shopInfoService.selectById(shopUserRelationDTO.getShopId());
                    }
            }
            if (shopInfoDTO!=null){
                //没开户
                if (shopInfoDTO.getCiccStatus()==0){
                    HashMap<String, Object> umap = new HashMap<String, Object>();
                    umap.put("shopInfo",shopInfoDTO);
                    //获取session的Id
                    String sessionId = Md5Utils.encryption(userInfoDTO.getId() + "" + System.currentTimeMillis());
                    umap.put("sessionId", sessionId);
                    //保存session进redis
                    userLoginService.setUserBySessionId(userInfoDTO, sessionId);
                    umap.put("userLogin", userLoginDTO);
                    return new Result<>(CodeEnum.FAIL_200029,umap);
                }
                EntityWrapper<ShopReceiveCardDTO> shopReceiveCardDTOEntityWrapper = new EntityWrapper<>();
                shopReceiveCardDTOEntityWrapper.eq("shop_id",shopInfoDTO.getId());
                List<ShopReceiveCardDTO> cardDTOList = shopReceiveCardService.selectList(shopReceiveCardDTOEntityWrapper);
                if (cardDTOList!=null&& cardDTOList.size()>0){
                    List<ShopReceiveCardDTO> receiveCardDTOList = new ArrayList<>();
                    for (ShopReceiveCardDTO shopReceiveCardDTO : cardDTOList) {
                        if (shopReceiveCardDTO.getActiveStatus()==1){
                            receiveCardDTOList.add(shopReceiveCardDTO);
                        }
                    }
                    if (!(receiveCardDTOList!=null && receiveCardDTOList.size()>0)){
                        HashMap<String, Object> umap = new HashMap<String, Object>();
                        umap.put("shopInfo",shopInfoDTO);
                        //获取session的Id
                        String sessionId = Md5Utils.encryption(userInfoDTO.getId() + "" + System.currentTimeMillis());
                        umap.put("sessionId", sessionId);
                        //保存session进redis
                        userLoginService.setUserBySessionId(userInfoDTO, sessionId);
                        umap.put("userLogin", userLoginDTO);
                        return new Result<>(CodeEnum.FAIL_200042,umap);
                    }
                }else {
                    HashMap<String, Object> umap = new HashMap<String, Object>();
                    umap.put("shopInfo",shopInfoDTO);
                    //获取session的Id
                    String sessionId = Md5Utils.encryption(userInfoDTO.getId() + "" + System.currentTimeMillis());
                    umap.put("sessionId", sessionId);
                    //保存session进redis
                    userLoginService.setUserBySessionId(userInfoDTO, sessionId);
                    umap.put("userLogin", userLoginDTO);
                    return new Result<>(CodeEnum.FAIL_200042,umap);
                }
            }

        }

        map.put("userInfo", userInfoDTO);

        UserImDTO buyUserIm = userLoginService.getImById(userInfoDTO.getId(), 0);
        map.put("buyIm", buyUserIm);

        UserImDTO sellUserImDTO = userLoginService.getImById(userInfoDTO.getId(), 1);
        map.put("sellIm", sellUserImDTO);

        ShopInfoDTO shopInfoDTO = userLoginService.getShopInfo(userInfoDTO.getId());
        if (shopInfoDTO != null && shopInfoDTO.getBuyerStatus() == 1) {
            //买家状态 0 未冻结，1冻结
            return new Result<>(CodeEnum.FAIL_USER_200004);
        }
        map.put("shopInfo", shopInfoDTO);
        userLoginService.resetSession(userInfoDTO.getId());
        //获取session的Id
        String sessionId = Md5Utils.encryption(userInfoDTO.getId() + "" + System.currentTimeMillis());
        map.put("sessionId", sessionId);
        //保存session进redis
        userLoginService.setUserBySessionId(userInfoDTO, sessionId);
        return new Result<>(map);
    }

    @ApiOperation(value = "小程序登录", notes = "小程序登录")
    @PostMapping(value = "/loginWX")
    public Result<HashMap<String, Object>> loginWX(UserLoginBO userLoginBO) {
        if (StringUtils.isEmpty(userLoginBO.getAccount()) || StringUtils.isEmpty(userLoginBO.getPassword())) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        HashMap<String, Object> map = new HashMap<String, Object>();
        UserLoginDTO userLoginDTO = userLoginService.getLogin(userLoginBO);
        if (userLoginDTO == null) {
            return new Result<>(CodeEnum.FAIL_USER_200003);
        }

        //验证是否被锁定
        if (userLoginDTO.getStatus().equals(1)) {
            Result result = new Result();
            result.setCode(1009);
            result.setMsg("您的账号已被锁定。请" + iSysDictService.getByName(UserConstant.UNLOCK_TIME).getValue() + "分钟后登录，或者联系管理员");
            return result;
        }
        userLoginDTO.setPassword(null);
        userLoginDTO.setSalt(null);
        map.put("userLogin", userLoginDTO);

        UserInfoDTO userInfoDTO = userLoginService.getUserInfo(userLoginBO);

        if (userInfoDTO == null) {
            //减少验证次数
            int verifyNum = userLoginService.lowerVerifyNum(userLoginDTO);
            Result result = new Result();
            if (verifyNum <= 0) {
                result.setCode(1009);
                result.setMsg("您的账号已被锁定。请" + iSysDictService.getByName(UserConstant.UNLOCK_TIME).getValue() + "分钟后登录，或者联系管理员");
                return result;
            } else {
                result.setCode(CodeEnum.FAIL_Login_1002.getCode());
                result.setMsg(CodeEnum.FAIL_Login_1002.getMsg() + "。剩余" + verifyNum + "次验证次数。次数使用完后将锁定您的账号");
            }
            return result;
        }

        if (userInfoDTO.getEmailActive() == 0) {
            HashMap<String, Object> umap = new HashMap<String, Object>();
            umap.put("userId", userInfoDTO.getId());
            umap.put("email", userLoginDTO.getEmail());
            return new Result<>(CodeEnum.FAIL_USER_200005.getCode(), CodeEnum.FAIL_USER_200005.getMsg(), umap);
        }

        if (userInfoDTO.getStatus() == 4 || userInfoDTO.getStatus() == 5) {
            //状态 0创建，1审核通过，2审核打回，3审核不通过,4冻结、5删除
            return new Result<>(CodeEnum.FAIL_USER_200004);
        }

        map.put("userInfo", userInfoDTO);

        UserImDTO buyUserIm = userLoginService.getImById(userInfoDTO.getId(), 0);
        map.put("buyIm", buyUserIm);

        UserImDTO sellUserImDTO = userLoginService.getImById(userInfoDTO.getId(), 1);
        map.put("sellIm", sellUserImDTO);

        ShopInfoDTO shopInfoDTO = userLoginService.getShopInfo(userInfoDTO.getId());
        if (shopInfoDTO != null && shopInfoDTO.getBuyerStatus() == 1) {
            //买家状态 0 未冻结，1冻结
            return new Result<>(CodeEnum.FAIL_USER_200004);
        }
        map.put("shopInfo", shopInfoDTO);
        userLoginService.resetSession(userInfoDTO.getId());
        //获取session的Id
        String sessionId = Md5Utils.encryption(userInfoDTO.getId() + "" + System.currentTimeMillis());
        map.put("sessionId", sessionId);
        //保存session进redis
        userLoginService.setUserBySessionId(userInfoDTO, sessionId);
        return new Result<>(map);
    }
    @ApiOperation(value = "免密登录", notes = "免密登录")
    @PostMapping(value = "/loginMobile")
    public Result<HashMap<String, Object>> loginMobile(UserLoginBO userLoginBO, HttpServletRequest request) {
        HashMap<String, Object> map = new HashMap<String, Object>();

        UserLoginDTO userLoginDTO = userLoginService.getLogin(userLoginBO);
        if (userLoginDTO == null) {
            return new Result<>(CodeEnum.FAIL_USER_200003);
        }

        //验证是否被锁定
        if (userLoginDTO.getStatus().equals(1)) {
            Result result = new Result();
            result.setCode(1009);
            result.setMsg("您的账号已被锁定。请" + iSysDictService.getByName(UserConstant.UNLOCK_TIME).getValue() + "分钟后登录，或者联系管理员");
            return result;
        }

        userLoginDTO.setPassword(null);
        userLoginDTO.setSalt(null);
        map.put("userLogin", userLoginDTO);
        // 验证类型 0注册验证码，1登录验证码，2修改密码验证码，3更改绑定手机验证码，4支付验证码，5保证金修改
        boolean flag_sms = userLoginService.getSmsCaptcha(userLoginBO.getAccount(), userLoginBO.getCaptcha(), 1);
        if (!flag_sms) {
            //减少验证次数
            int verifyNum = userLoginService.lowerVerifyNum(userLoginDTO);
            Result result = new Result();
            if (verifyNum <= 0) {
                result.setCode(1009);
                result.setMsg("您的账号已被锁定。请" + iSysDictService.getByName(UserConstant.UNLOCK_TIME).getValue() + "分钟后登录，或者联系管理员");
            } else {
                result.setCode(CodeEnum.FAIL_Login_1004.getCode());
                result.setMsg(CodeEnum.FAIL_Login_1004.getMsg() + "。剩余" + verifyNum + "次验证次数。次数使用完后将锁定您的账号");
            }
            return result;
        }

        UserInfoDTO userInfoDTO = userLoginService.getLoginMobile(userLoginDTO);

        if (userInfoDTO.getEmailActive() == 0) {
            HashMap<String, Object> umap = new HashMap<String, Object>();
            umap.put("userId", userInfoDTO.getId());
            umap.put("email", userLoginDTO.getEmail());
            return new Result<>(CodeEnum.FAIL_USER_200005.getCode(), CodeEnum.FAIL_USER_200005.getMsg(), umap);
        }

        if (userInfoDTO.getStatus() == 4 || userInfoDTO.getStatus() == 5) {
            //状态 0创建，1审核通过，2审核打回，3审核不通过,4冻结、5删除
            return new Result<>(CodeEnum.FAIL_USER_200004);
        }
        if (userInfoDTO.getStatus() == 1) {
            //状态 0创建，1审核通过，2审核打回，3审核不通过,4冻结、5删除
            EntityWrapper<ShopInfoDTO> shopInfoDTOEntityWrapper = new EntityWrapper<>();
            shopInfoDTOEntityWrapper.eq("user_id",userInfoDTO.getId());
            ShopInfoDTO shopInfoDTO = shopInfoService.selectOne(shopInfoDTOEntityWrapper);
            if (shopInfoDTO!=null){
                //没开户
                if (shopInfoDTO.getCiccStatus()==0){
                    HashMap<String, Object> umap = new HashMap<String, Object>();
                    umap.put("shopInfo",shopInfoDTO);
                    //获取session的Id
                    String sessionId = Md5Utils.encryption(userInfoDTO.getId() + "" + System.currentTimeMillis());
                    umap.put("sessionId", sessionId);
                    //保存session进redis
                    userLoginService.setUserBySessionId(userInfoDTO, sessionId);
                    umap.put("userLogin", userLoginDTO);
                    return new Result<>(CodeEnum.FAIL_200029,umap);
                }
                EntityWrapper<ShopReceiveCardDTO> shopReceiveCardDTOEntityWrapper = new EntityWrapper<>();
                shopReceiveCardDTOEntityWrapper.eq("shop_id",shopInfoDTO.getId());
                List<ShopReceiveCardDTO> cardDTOList = shopReceiveCardService.selectList(shopReceiveCardDTOEntityWrapper);
                if (cardDTOList!=null&& cardDTOList.size()>0){
                    List<ShopReceiveCardDTO> receiveCardDTOList = new ArrayList<>();
                    for (ShopReceiveCardDTO shopReceiveCardDTO : cardDTOList) {
                        if (shopReceiveCardDTO.getActiveStatus()==1){
                            receiveCardDTOList.add(shopReceiveCardDTO);
                        }
                    }
                    if (!(receiveCardDTOList!=null && receiveCardDTOList.size()>0)){
                        HashMap<String, Object> umap = new HashMap<String, Object>();
                        umap.put("shopInfo",shopInfoDTO);
                        //获取session的Id
                        String sessionId = Md5Utils.encryption(userInfoDTO.getId() + "" + System.currentTimeMillis());
                        umap.put("sessionId", sessionId);
                        //保存session进redis
                        userLoginService.setUserBySessionId(userInfoDTO, sessionId);
                        umap.put("userLogin", userLoginDTO);
                        return new Result<>(CodeEnum.FAIL_200042,umap);
                    }
                }else {
                    HashMap<String, Object> umap = new HashMap<String, Object>();
                    umap.put("shopInfo",shopInfoDTO);
                    //获取session的Id
                    String sessionId = Md5Utils.encryption(userInfoDTO.getId() + "" + System.currentTimeMillis());
                    umap.put("sessionId", sessionId);
                    //保存session进redis
                    userLoginService.setUserBySessionId(userInfoDTO, sessionId);
                    umap.put("userLogin", userLoginDTO);
                    return new Result<>(CodeEnum.FAIL_200042,umap);
                }
            }

        }

        map.put("userInfo", userInfoDTO);

        UserImDTO buyUserIm = userLoginService.getImById(userInfoDTO.getId(), 0);
        map.put("buyIm", buyUserIm);

        UserImDTO sellUserImDTO = userLoginService.getImById(userInfoDTO.getId(), 1);
        map.put("sellIm", sellUserImDTO);

        ShopInfoDTO shopInfoDTO = userLoginService.getShopInfo(userInfoDTO.getId());
        if (shopInfoDTO != null && shopInfoDTO.getBuyerStatus() == 1) {
            //买家状态 0 未冻结，1冻结
            return new Result<>(CodeEnum.FAIL_USER_200004);
        }
        map.put("shopInfo", shopInfoDTO);
        //返回sessionId
        userLoginService.resetSession(userInfoDTO.getId());
        String sessionId = Md5Utils.encryption(userInfoDTO.getId() + "" + System.currentTimeMillis());
        map.put("sessionId", sessionId);
        //保存session进redis
        userLoginService.setUserBySessionId(userInfoDTO, sessionId);
        return new Result<>(map);
    }

    @ApiOperation(value = "退出登录", notes = "退出登录")
    @PostMapping(value = "/quit")
    public Result quit(HttpServletRequest httpServletRequest) {
        return userLoginService.updateQuit(httpServletRequest.getHeader("sessionId"));
    }

    @ApiOperation(value = "修改密码", notes = "修改密码")
    @PostMapping(value = "/updatePassWord")
    public Result updatePassword(UserLoginBO userLoginBO) {
        UserInfoVO vo = userInfoService.getVo(userLoginBO.getId());
        if (null == userLoginBO.getType() || StringUtils.isEmpty(userLoginBO.getPassword()) || userLoginBO.getPassword().length() < 32) {
            return new Result<>(CodeEnum.FAIL_100002);
        }

        //验证码0注册验证码，1登录验证码，2修改密码验证码，3更改绑定手机验证码，4支付验证码，5保证金修改
        if (userLoginBO.getType().equals(0)) {
            if(null != vo){
                userLoginBO.setAccount(vo.getMobile());
            }
            if (!userLoginService.getSmsCaptcha(userLoginBO.getAccount(), userLoginBO.getCaptcha(), 2)) {
                return new Result<>(CodeEnum.FAIL_Login_1004);
            }
        } else {
            if(null != vo) {
                userLoginBO.setAccount(vo.getEmail());
            }
            if (!userLoginService.getEmailCaptcha(userLoginBO.getAccount(), userLoginBO.getCaptcha())) {
                return new Result<>(CodeEnum.FAIL_Login_1005);
            }
        }

        return userLoginService.updatePassword(userLoginBO);
    }

    @ApiOperation(value = "修改密码", notes = "修改密码")
    @PostMapping(value = "/updatePassWordByUserId")
    public Result updatePassWordByUserId(UserLoginBO userLoginBO) {

        if (null == userLoginBO.getType() || StringUtils.isEmpty(userLoginBO.getPassword()) || userLoginBO.getPassword().length() < 32) {
            return new Result<>(CodeEnum.FAIL_100002);
        }

        //验证码0注册验证码，1登录验证码，2修改密码验证码，3更改绑定手机验证码，4支付验证码，5保证金修改
        if (userLoginBO.getType().equals(0)) {

            if (!userLoginService.getSmsCaptcha(userLoginBO.getAccount(), userLoginBO.getCaptcha(), 2)) {
                return new Result<>(CodeEnum.FAIL_Login_1004);
            }
        } else {

            if (!userLoginService.getEmailCaptcha(userLoginBO.getAccount(), userLoginBO.getCaptcha())) {
                return new Result<>(CodeEnum.FAIL_Login_1005);
            }
        }

        return userLoginService.updatePassword(userLoginBO);
    }

    @ApiOperation(value = "修改子账号密码", notes = "修改子账号密码")
    @PostMapping(value = "/updateSubPassWord")
    public Result updateSubPassWord(UserLoginBO userLoginBO) {
        if (StringUtils.isEmpty(userLoginBO.getPassword()) || userLoginBO.getPassword().length() < 32) {
            return new Result<>(CodeEnum.FAIL_100002);
        }

        if (!userLoginService.getSmsCaptcha(userLoginBO.getMainMobile(), userLoginBO.getCaptcha(), 2)) {
            return new Result<>(CodeEnum.FAIL_Login_1004);
        }

        return userLoginService.updatePassword(userLoginBO);
    }

    @ApiOperation(value = "修改联系方式", notes = "修改联系方式")
    @PostMapping(value = "/updateContactInfo")
    public Result updateContactInfo(UserLoginUpdateBO bo) {

        if (StringUtils.isEmpty(bo.getEmail()) || StringUtils.isEmpty(bo.getMobile()) || StringUtils.isEmpty(bo.getEmailCaptcha()) || StringUtils.isEmpty(bo.getMobileCaptcha())) {
            return new Result<>(CodeEnum.FAIL_100002);
        }

        Wrapper<UserLoginDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", bo.getUserId());
        UserLoginDTO dto = userLoginService.selectOne(wrapper);
        if (dto == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }

        //验证手机号是否重复 可以和自己的手机号重复
        if (userLoginService.updateByMobile(bo.getMobile()) && !dto.getMobile().equals(bo.getMobile())) {
            return new Result<>(CodeEnum.FAIL_AddUser_3004);
        }

        log.info("bo_tostring========="+bo.toString());
        //验证邮箱是否重复 可以和自己的邮箱重复
        if (userLoginService.updateByEmail(bo.getEmail()) && !dto.getEmail().equals(bo.getEmail())) {
            return new Result<>(CodeEnum.FAIL_AddUser_3005);
        }

        if (!userLoginService.getEmailCaptcha(bo.getEmail(), bo.getEmailCaptcha())) {
            return new Result<>(CodeEnum.FAIL_Login_1005);
        }

        if(StringUtils.isNotEmpty(bo.getOldEmailCaptcha())){
            if (!userLoginService.getEmailCaptcha(dto.getEmail(), bo.getOldEmailCaptcha())) {
                return new Result<>(CodeEnum.FAIL_Login_1005);
            }
        }


        //验证类型 0注册验证码，1登录验证码，2修改密码验证码，3更改绑定手机验证码，4支付验证码，5保证金修改
        if (!userLoginService.getSmsCaptcha(bo.getMobile(), bo.getMobileCaptcha(), 3)) {
            log.info("1-------------"+bo.getMobile()+"----------"+bo.getMobileCaptcha());
            return new Result<>(CodeEnum.FAIL_Login_1004);
        }

        //验证历史手机号 0注册验证码，1登录验证码，2修改密码验证码，3更改绑定手机验证码，4支付验证码，5保证金修改
        if(StringUtils.isNotEmpty(bo.getOldMobileCaptcha())){
            if (!userLoginService.getSmsCaptcha(dto.getMobile(), bo.getOldMobileCaptcha(), 3)) {
                log.info("2-------------"+dto.getMobile()+"----------"+bo.getMobileCaptcha());
                return new Result<>(CodeEnum.FAIL_Login_1004);
            }
        }

        boolean flag = userLoginService.updateContactInfo(bo);
        if (flag) {
            return new Result<>(CodeEnum.SUCCESS);
        } else {
            return new Result<>(CodeEnum.FAIL);
        }
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找", notes = "根据id查找")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    public Result<UserLoginDTO> get(@RequestParam("id") Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }

        UserLoginDTO dto = userLoginService.selectById(id);

        return new Result<>(dto);
    }

    @PostMapping("/getActiveByEmail")
    @ApiOperation(value = "根据email获取邮箱激活状态", notes = "根据email获取邮箱激活状态")
    @ApiImplicitParam(name = "email", value = "email", required = true, paramType = "query", dataType = "String")
    public Result getActiveByEmail(@RequestParam("email") String email) {
        if (StringUtils.isEmpty(email)) {
            return new Result<>(CodeEnum.FAIL_100002);
        }

        boolean flag = userLoginService.getActiveByEmail(email);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL);
        }

    }

    @PostMapping("/getByMobile")
    @ApiOperation(value = "根据手机号获取是否已注册", notes = "根据手机号获取是否已注册")
    @ApiImplicitParam(name = "mobile", value = "mobile", required = true, paramType = "query", dataType = "String")
    public Result getByMobile(@RequestParam("mobile") String mobile) {
        if (StringUtils.isEmpty(mobile)) {
            return new Result<>(CodeEnum.FAIL_100002);
        }

        //验证手机号是否重复
        if (userLoginService.updateByMobile(mobile)) {
            return new Result<>(CodeEnum.FAIL_AddUser_3004);
        }
        return new Result<>();
    }

    @PostMapping("/getVerify")
    @ApiOperation(value = "获取滑动图片验证码", notes = "获取图片验证码")
    public Result<HashMap<String, Object>> getVerify(String randomStr) {
        if (StringUtils.isEmpty(randomStr)) {
            return new Result<>(CodeEnum.FAIL_100002);
        }

        HashMap<String, Object> resultMap = RandomValidateCodeUtil.createImage(RandomValidateCodeUtil.IMAGEPATH);
        Integer xWidth = Integer.valueOf(resultMap.get("xWidth").toString());
        resultMap.remove("xWidth");

        boolean flag = userLoginService.setVerifyWidth(RandomValidateCodeUtil.DEFAULT_CODE_KEY + randomStr, xWidth);

        if (flag) {
            return new Result<>(resultMap);
        } else {
            return new Result<>(CodeEnum.FAIL_Login_1007);
        }
    }

    @ApiOperation(value = "添加用户登录表", notes = "添加用户登录表")
    @PostMapping(value = "/registered")
    public Result registered(String loginVO) {
        if (StringUtils.isEmpty(loginVO)) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        return userLoginService.saveRegistered(loginVO);
    }
}
